<template>
  <el-dialog class="sellDialog" :visible.sync="visible" title="新增" width="500px" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submit()" label-width="120px">
      <el-form-item label="购买车辆" prop="carNumber">
        <el-select class="s_input" v-model="dataForm.carNumber" filterable placeholder="请选择" @change="changeCar">
          <el-option v-for="item in carList" :key="item.id" :label="item.carModel" :value="item.carNumber">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆编号">
        <el-input class="s_input" v-model="dataForm.carNumber" disabled></el-input>
      </el-form-item>
      <el-form-item label="车型">
        <el-input class="s_input" v-model="dataForm.carModel" disabled></el-input>
      </el-form-item>
      <el-form-item label="等级">
        <el-input class="s_input" v-model="dataForm.level" disabled></el-input>
      </el-form-item>
      <el-form-item label="购买保险" prop="carInsuranceIds">
        <el-select class="s_input" multiple v-model="dataForm.carInsuranceIds">
          <el-option v-for="(item, index) in insurancename" :key="index" :label="item.insuranceName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最终成交价" prop="finalPrices">
        <el-input class="s_input" v-model="dataForm.finalPrices"></el-input>
      </el-form-item>
      <el-form-item label="客户姓名" prop="customerName">
        <el-input class="s_input" v-model="dataForm.customerName"></el-input>
      </el-form-item>
      <el-form-item label="客户联系方式" prop="customerPhone">
        <el-input class="s_input" v-model="dataForm.customerPhone"></el-input>
      </el-form-item>
      <el-form-item label="购车备注">
        <el-input class="s_input" v-model="dataForm.remarks"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary" @click="submit()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {},
      insurancename: [],
      carList: []
    }
  },
  computed: {
    dataRule () {
      return {
        carNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        carInsuranceIds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        finalPrices: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        customerPhone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 显示弹窗
    init () {
      this.visible = true
      this.getinsurancename()
      this.getCarNum()
      this.dataForm = {
        carNumber: '',
        carModel: '',
        level: '',
        carInsuranceIds: [],
        finalPrices: '',
        customerName: '',
        customerPhone: '',
        remarks: ''
      }
    },
    // 修改车辆
    changeCar (e) {
      let item = this.carList.find(item => item.carNumber === e)
      this.dataForm.carModel = item.carModel
      this.dataForm.level = item.level
    },
    // 表单提交
    submit: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post('/cardeal/add', this.dataForm).then(({ data: res }) => {
          if (res.code === 0) {
            this.$message.success('保存成功!')
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((error) => {
          return this.$message.error(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 获取车辆下拉列表
    getCarNum () {
      this.$http.get('/carinfo/getCarNum').then(({ data: res }) => {
        let { code, carNums } = res
        this.carList = []
        if (code === 0) {
          this.carList = carNums
        }
      })
    },
    // 获取保险下拉框
    getinsurancename () {
      this.$http.get('/carinsurance/getinsurancename').then(({ data: res }) => {
        let { code, dealInsuranceVOList } = res
        this.insurancename = []
        if (code === 0) {
          this.insurancename = dealInsuranceVOList
        }
      })
    }
  }
}
</script>
<style lang="scss">
.sellDialog{
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
  .s_input{
    width: 80%;
    margin-right: 10px;
  }
}
</style>
